// pages/xinzengshouhuodizhi/xinzengshouhuodizhi.js
const app = getApp();
import {
  areaList
} from '@vant/area-data';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList,
    CUSTOMER_TEL: '',
    shengji: '',
    shiji: '',
    xianji: '',
    CONSIGNEE_ADDRESS: '',
    USER_ASSETS_NAME: '',
    checked: false,
    moren: 2,
  },

  onChangehua({
    detail
  }) {
    // 需要手动对 checked 状态进行更新
    this.setData({
      checked: detail
    });
    if (this.data.checked == false) {
      this.setData({
        moren: 2

      })
    } else {

      this.setData({
        moren: 1
      })

    }
    console.log(this.data.moren);

  },

  quxiao() {
    this.setData({
      show: false
    });
  },

  queding(e) {
    console.log(e);
    console.log(e.detail.values[0].name);
    console.log(e.detail.values[1].name);
    console.log(e.detail.values[2].name);
    this.setData({
      show: false,
      shengji: e.detail.values[0].name,
      shiji: e.detail.values[1].name,
      xianji: e.detail.values[2].name,
    });
  },


  inptext(e) {
    console.log(e);
    this.setData({
      CONSIGNEE_ADDRESS: e.detail.value
    })
  },
  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },



  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    this.setData({
      mobile: value,
    })
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        CUSTOMER_TEL: ''
      })
    } else if (this.data.mobile.length < 11) {
      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        CUSTOMER_TEL: ''
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        CUSTOMER_TEL: ''
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },






  save() {

    this.setData({
      CUSTOMER_ID: app.globalData.CUSTOMER_ID
    });
    if ( this.data.USER_ASSETS_NAME == '' ||   this.data.CUSTOMER_TEL == '' || this.data.shengji == '' || this.data.CONSIGNEE_ADDRESS == '') {
      wx.showToast({
        title: '请完善地址信息',
        icon: 'error',
        duration: 2000
      })
    } else {
      wx.request({
        url: app.globalData.interface+'/newVersion/API_2071_saveShippingAddress',
        method: 'POST',
        data: {
          USER_ASSETS_NAME: this.data.USER_ASSETS_NAME,
          CUSTOMER_TEL: this.data.CUSTOMER_TEL,
          SHOP_TYPE: this.data.moren,
          PROVINCE_NAME: this.data.shengji,
          CITY_NAME: this.data.shiji,
          DISTRICT_NAME: this.data.xianji,
          ADDRESS: this.data.CONSIGNEE_ADDRESS,

          OPEN_ID: this.data.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami,
          CUSTOMER_ID: this.data.CUSTOMER_ID,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res.data);

          if (res.data.result == 'success') {

            wx.showToast({
              title: '地址添加成功！',
              icon: 'success',
              duration: 1500
            })

          }else{
            wx.showToast({
              title: res.data.data,
              icon: 'error',
              duration: 2000
            })


          }
        }
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})